package com.oa.controller.user;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.oa.pojo.TodoList;
import com.oa.service.user.TodoService;
import com.oa.vo.PageResult;
import com.oa.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Author: 一拳超人
 * @Date: 2021/10/12 15:26
 */
@RestController
@CrossOrigin
@RequestMapping("/user/todo")
public class TodoController {
    @Autowired
    private TodoService todoService;

    @PostMapping("/addTodo")
    public SysResult addNewTodo(@RequestBody TodoList todoList) {
        return SysResult.success(todoService.addNerTodo(todoList));
    }

    @GetMapping("/getAllTodoList")
    public SysResult getAllTodoList(PageResult pageResult) {
        return SysResult.success(todoService.getAllTodoList(pageResult));
    }

    @GetMapping("/getForTodoList")
    public SysResult getGoingTodoList(PageResult pageResult) {
        return SysResult.success(todoService.getGoingTodoList(pageResult));
    }

    @GetMapping("/getEndTodoList")
    public SysResult getEndTodoList(PageResult pageResult) {
        return SysResult.success(todoService.getEndTodoList(pageResult));
    }

    @PutMapping("/updateTodoStatus")
    public SysResult updateTodoStatus(@RequestBody TodoList todoList) {
        return SysResult.success(todoService.updateTodoStatus(todoList));
    }

    @DeleteMapping("/deleteTodo/{id}")
    public SysResult deleteTodo(TodoList todoList) {
        return SysResult.success(todoService.deleteTodo(todoList));
    }

    @GetMapping("/getTodoInfo")
    public SysResult getTodoInfo(Integer id) {
        return SysResult.success(todoService.getTodoInfo(id));
    }

    @PutMapping("/updateTodoInfo")
    public SysResult updateTodoInfo(@RequestBody TodoList todoList) {
        if (todoList.getEndTime() != null) {
            if (!todoList.getTodoStatus() && todoList.getEndTime().compareTo(new Date()) < 0) {
                todoList.setTodoStatus(true);
            }
            if (todoList.getEndTime().compareTo(todoList.getStartTime()) < 0) {
                return SysResult.fail("结束时间不得早于开始时间");
            }
        }
        return SysResult.success(todoService.updateTodoInfo(todoList));
    }
}
